﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Backend.DTO;
using Backend.BLL;
using System.Data;
using Backend;

namespace ReSeSSApplication
{
    public partial class ManageUsers : System.Web.UI.Page
    {
        UserBLL aUserBll = new UserBLL();
        UserDTO loggedInUserDTO = new UserDTO();

        private void Page_Load(object sender, EventArgs e)
        {
            AlertError.Visible = false;
            AlertSuccess.Visible = false;
            AlertInfo.Visible = false;

            loggedInUserDTO = (UserDTO)Session["LoggedInUserDTO"];
            RedirectUser(loggedInUserDTO);

            LoadUserGridView();
        }

        protected void UserGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string isActive = e.Row.Cells[4].Text;
                if (isActive == "True")
                {
                    e.Row.Cells[4].Text = "<span class='label label-success'>YES</span>";
                }
                else if (isActive == "False")
                {
                    e.Row.Cells[4].Text = "<span class='label label-important'>NO</span>";
                }

                string email = e.Row.Cells[2].Text;
                if (!string.IsNullOrEmpty(email) && Utility.IsValidEmail(email))
                {
                    e.Row.Cells[2].Text = "<a href='mailto:" + email + "'>" + email + "</a>";
                }
            }
        }

        protected void UserGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                HiddenField userIdHiddenField = (HiddenField)UserGridView.Rows[e.RowIndex].FindControl("UserId");

                int userId = Convert.ToInt32(userIdHiddenField.Value);
                if (userId > 0)
                {
                    aUserBll.DeleteUser(userId);
                    AlertSuccess.InnerText = "User deleted successfully";
                    AlertSuccess.Visible = true;

                    LoadUserGridView();
                }
            }
            catch(Exception ex)
            {
                AlertError.InnerText = ex.Message;
                AlertError.Visible = true;
            }
        }

        protected void UserGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                try
                {
                    int rowIndex = Convert.ToInt32(e.CommandArgument);
                    HiddenField userIdHiddenField = (HiddenField)UserGridView.Rows[rowIndex].FindControl("UserId");
                    Response.Redirect("RegisterUser.aspx?e=" + userIdHiddenField.Value);
                }
                catch (Exception ex)
                {
                    AlertError.InnerText = ex.Message;
                    AlertError.Visible = true;
                }
            }
        }

        protected void UserGridView_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[5].ColumnSpan = 2;
                e.Row.Cells.RemoveAt(6);
            }
        }

        private void LoadUserGridView()
        {
            List<UserDTO> listOfUsers = aUserBll.GetAllUser();
            UserGridView.DataSource = listOfUsers;
            UserGridView.DataBind();

            AlertInfo.InnerHtml = "Currently there are <span class='badge badge-info'>" + listOfUsers.Count() + "</span> user(s).";
            AlertInfo.Visible = true;
        }

        private void RedirectUser(UserDTO userDTO)
        {
            if (userDTO != null)
            {
                if (userDTO.UserType == "User")
                {
                    Response.Redirect("UserDashboard.aspx");
                }
            }
            else
            {
                Response.Redirect("Index.aspx");
            }
        }
    }
}